'''
1. 有道翻译md5加密

'''
import requests
import execjs
import random
import time
#获取sign
node = execjs.get()
e = "cat" #要翻译的单词
r = str(int(time.time() * 1000))
i =  r + str(int(random.random()*10))
ctx = node.compile(open('./文件/有道翻译.js', encoding='utf-8').read())
funcName = 'getSign("{0}", "{1}")'.format(e, i)
sign = ctx.eval(funcName)
print(sign)

url = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
heards = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36"
    ,"Cookie": "OUTFOX_SEARCH_USER_ID=933798852@10.110.96.159; JSESSIONID=aaaBwEse8_9J9_SLN5Bby; OUTFOX_SEARCH_USER_ID_NCOO=2110608109.6116455; fanyi-ad-id=305676; fanyi-ad-closed=1; ___rl__test__cookies=1650812010288"
    ,"Referer": "https://fanyi.youdao.com/"
}
data = {
    "i": e,
    "from": "AUTO",
    "to": "AUTO",
    "smartresult": "dict",
    "client": "fanyideskweb",
    "salt": i,
    "sign": sign,
    "lts": r,
    "bv": "ac3968199d18b7367b2479d1f4938ac2",
    "doctype": "json",
    "version": "2.1",
    "keyfrom": "fanyi.web",
    "action": "FY_BY_REALTlME",
}

resp = requests.post(url=url, headers=heards, data=data)

info = resp.json()

print(info)